package br.com.caelum.vraptor.controller;

import java.util.List;
import javax.inject.Inject;
import br.com.caelum.vraptor.Controller;
import br.com.caelum.vraptor.Result;
import br.com.caelum.vraptor.dao.AlunoDAO;
import br.com.caelum.vraptor.entidades.Aluno;
import br.com.caelum.vraptor.componentes.AlunoWeb;

@Controller
public class AlunoController {
	
	@Inject
	private Result result;
	
	@Inject
	private AlunoDAO dao;
	@Inject
	private AlunoWeb alunoWeb;

	public void add(){
		
	}
	
	public void login(){
		
		
	}
	public List<Aluno> list(){
		return dao.find();	
	}
	
	
	public void save(Aluno aluno){
		
		dao.save(aluno);
		
		result.redirectTo(AlunoController.class).login();
	}
	
	public Aluno edit(Integer id){
		return dao.find(id);
	}
	public void logar(String email, String senha){
		Aluno n = dao.find(email, senha);
		if(n==null){result.redirectTo(AlunoController.class).login();}
		alunoWeb.login(n);
		result.redirectTo(IndexController.class).index();
		
	}
	
	public void delete(int id){
		Aluno aluno = new Aluno();
		aluno.setId(id);
		dao.delete(aluno);
		result.redirectTo(this).list();
	}
	
}
